package com.blt.car.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blt.car.MainActivity;
import com.blt.car.R;
import com.blt.car.base.BaseActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.utils.Uiutils;
import com.blt.car.widget.CountDownTimerUtils;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.login.LoginApi;
import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/15.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    public static final String ACTIVITY_NAME = "activity_name";

    private EditText mPhone;
    private EditText etVerCode;
    private EditText inputPwd;
    private EditText confirmPwd;

    private EditText inviteEt;
    private Button getVerCode;


    private int inType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            inType = intent.getExtras().getInt(ACTIVITY_NAME);
        }


    }

    @Override
    protected void initView(ViewHolder holder) {
        mPhone = holder.get(R.id.register_et_user);
        inviteEt = holder.get(R.id.register_et_invite_code);
        getVerCode = holder.get(R.id.register_btn_getcode);
        Button register = holder.get(R.id.register_btn_regist);
        holder.get(R.id.register_back);
        etVerCode = holder.get(R.id.register_et_vercode);
        TextView proxy = holder.get(R.id.register_proxy);
        inputPwd = holder.get(R.id.register_et_put_pwd);
        confirmPwd = holder.get(R.id.register_et_confirm_pwd);
        holder.setOnClickListener(this, R.id.register_btn_getcode, R.id.register_btn_regist, R.id.register_back);

        if (inType == 1) {
            inviteEt.setVisibility(View.GONE);
            proxy.setVisibility(View.GONE);
            register.setText("确定");
        }

    }
    //首先拿到验证码，拿到验证码之后输入密码，然后把密码和手机号码一起发到服务端，进行注册的同时也进行登陆

    @Override
    public void onClick(View v) {
        String phone = mPhone.getText().toString();
        switch (v.getId()) {
            case R.id.register_btn_getcode:
                if (TextUtils.isEmpty(phone)) {
                    toastShort("手机号为空");
                    return;
                }
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getVerCode, 60000, 1000);
                mCountDownTimerUtils.start();
                CoreManager.getCore(LoginApi.class).getVerCode(phone);
                break;
            case R.id.register_btn_regist:
                String verCode = etVerCode.getText().toString();
                String inPwd = inputPwd.getText().toString();
                String conPwd = confirmPwd.getText().toString();
                if (TextUtils.isEmpty(inPwd) || TextUtils.isEmpty(conPwd)) {
                    toastShort("密码不能为空");
                    return;
                }
                if (!inPwd.equals(conPwd)) {
                    toastShort("2次密码不一样");
                    return;
                }

                Uiutils.progressShow(this);
                if (inType == 1) {
                    findPassword(phone, inPwd, verCode);
                } else {
                    register(phone, inPwd, verCode);
                }
                break;
            case R.id.register_back:
                finish();
                break;
        }
    }

    private void findPassword(String phone, String inPwd, String verCode) {
        CoreManager.getCore(LoginApi.class).forgetPassword(phone, inPwd, verCode);
    }

    private void register(String phone, String inPwd, String verCode) {
        CoreManager.getCore(LoginApi.class).register(verCode, phone, inPwd);
    }

    @CoreEvent(coreClientClass = LoginApi.class)
    public void onGetVerCode() {
        toastShort("获取验证码成功 ");
    }
//    @CoreEvent(coreClientClass = LoginApi.class)
//    public void onRegister() {
//        toastShort("注册成功");
//        Uiutils.progressCancel();
//        openActivity(MainActivity.class);
//        finish();
//    }

    @CoreEvent(coreClientClass = LoginApi.class)
    public void onRegister(String msg) {
        Logger.d("onRegister" + msg);
        toastShort(msg);
        Uiutils.progressCancel();
        openActivity(MainActivity.class);
        finish();
    }

}
